#include "table.h"
#include "symbol.h"

StringTable*
StringTable::instance()
{
	static StringTable inst;
	return &inst;
}

int 
StringTable::get(size_t i,std::string &str)
{
	if(i<_table.size())
	{
		str=_table[i];
		return i;
	}
	return -1;
}

int 
StringTable::find(std::string const &str)
{
	for(size_t i=0, size=_table.size();i<size;++i)
	{
		if(str==_table[i])
			return i;
	}
	return -1;
}

int 
StringTable::insert(std::string const &str)
{
	if(-1==find(str))
	{
		_table.push_back(str);
		return _table.size()-1;
	}
	return -1;
}

int 
StringTable::replace(std::string const &str)
{
	int index = find(str);
	if(-1!=index)
	{
		return index;
	}
	_table.push_back(str);
	return _table.size()-1;	
}

int 
StringTable::replace(char const *str)
{
	return replace(std::string(str));
}


SymbolTable * 
SymbolTable::instance()
{
	static SymbolTable inst;
	return &inst;
}

SymbolTable::~SymbolTable()
{
	for(size_t i=0 ; i<_symbols.size() ; ++i)
	{
		delete _symbols[i];
	}

	_symbols.clear();
}

int 
SymbolTable::insert(Symbol *s)
{
	_symbols.push_back(s);
	return 0;
}

#include <iostream>

int 
SymbolTable::debug()
{
	std::cout<<"-----------------------DEBUG----------------------\n";
	for(size_t i=0 ; i<_symbols.size(); ++i)
	{
		std::cout << "----" << _symbols[i]->name().c_str() << "----\n";	
	}
	return 0;
}